package com.hsw.habit.service.impl;

import com.hsw.habit.mapper.NoteItemMapper;
import com.hsw.habit.model.NoteGroups;
import com.hsw.habit.model.NoteItem;
import com.hsw.habit.model.NoteItemExample;
import com.hsw.habit.model.bean.ListResponse;
import com.hsw.habit.service.NoteGroupService;
import com.hsw.habit.service.NoteItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NoteItemServiceImpl implements NoteItemService {

    @Resource
    NoteItemMapper noteItemMapper;

    @Resource
    NoteGroupService groupService;

    private final static int PAGE_SIZE = 20;


    @Override
    public ListResponse<NoteItem> findNoteItemByUserId(Integer userId, Integer startRow) {
        NoteItemExample example = new NoteItemExample();
        NoteItemExample.Criteria criteria = example.createCriteria();
        criteria.andIsdeletedEqualTo((short) 0);
        criteria.andUseridEqualTo(userId);
        example.setPageSize(PAGE_SIZE);
        if (null == startRow) {
            startRow = 0;
        }
        example.setStartRow(startRow);
        ListResponse<NoteItem> listResponse = new ListResponse<>();
        List<NoteItem> list = noteItemMapper.selectByExample(example);
        if (null == list || list.size() < PAGE_SIZE) {
            listResponse.setEnd(true);
        } else {
            listResponse.setStartRow(startRow + PAGE_SIZE);
        }

        List<NoteGroups> noteGroups = groupService.selectGroupsByUserId(userId);


        if (null != list && list.size() > 0) {
            for (NoteItem noteItem1 : list) {
                for (NoteGroups noteGroups1 : noteGroups) {
                    if (noteItem1.getGroupId().equals(noteGroups1.getId())) {
                        noteItem1.setGroupName(noteGroups1.getGroupName());
                    }
                }
            }
        }

        listResponse.setList(list);
        return listResponse;
    }

    @Override
    public ListResponse<NoteItem> findNoteItemByGroupId(Integer groupId, Integer startRow) {
        NoteItemExample example = new NoteItemExample();
        NoteItemExample.Criteria criteria = example.createCriteria();
        criteria.andIsdeletedEqualTo((short) 0);
        criteria.andGroupIdEqualTo(groupId);
        example.setPageSize(PAGE_SIZE);
        if (null == startRow) {
            startRow = 0;
        }
        example.setStartRow(startRow);
        ListResponse<NoteItem> listResponse = new ListResponse<>();
        List<NoteItem> list = noteItemMapper.selectByExample(example);
        if (null == list || list.size() < PAGE_SIZE) {
            listResponse.setEnd(true);
        } else {
            listResponse.setStartRow(startRow + PAGE_SIZE);
        }

        NoteGroups groupName = groupService.selectGroupById(groupId);

        if (null != list && list.size() > 0) {
            for (NoteItem noteItem1 : list) {
                noteItem1.setGroupName(groupName.getGroupName());
            }
        }

        listResponse.setList(list);
        return listResponse;
    }

    @Override
    public NoteItem findNoteById(Integer id) {
        NoteGroups noteGroups = groupService.selectGroupById(id);
        NoteItem noteItem = noteItemMapper.selectByPrimaryKey(id);
        noteItem.setGroupName(noteGroups.getGroupName());
        return noteItem;
    }

    private String getGroupNameById(Integer groupId,Integer userId) throws Exception {
        List<NoteGroups> noteGroups = groupService.selectGroupsByUserId(userId);
        for (NoteGroups noteGroups1 : noteGroups) {
            if (groupId.equals(noteGroups1.getId())) {
                return noteGroups1.getGroupName();
            }
        }
        return "";
    }

    @Override
    public int addNoteItem(NoteItem noteItem) {
        return noteItemMapper.insertSelective(noteItem);
    }

    @Override
    public int updateNoteItem(NoteItem noteItem) {
        return noteItemMapper.updateByPrimaryKeySelective(noteItem);
    }

    @Override
    public int delNoteItem(Integer id) {
        NoteItem noteItem = findNoteById(id);
        if (null == noteItem) {
            return 0;
        }
        noteItem.setIsDeleted((short) 1);
        return noteItemMapper.updateByPrimaryKeySelective(noteItem);
    }
}
